class_name AIBehavior
extends Node

const AI_TICK_FREQUENCY := 200

var ball : Ball
var player : Player
var opponent_detection_area : Area2D = null
var teammate_detection_area:Area2D = null

var time_since_last_ai_tick :=  Time.get_ticks_msec()


func _ready() -> void:
	time_since_last_ai_tick = Time.get_ticks_msec() + randi_range(0,AI_TICK_FREQUENCY)

func setup(context_player : Player , context_ball : Ball,context_opponent_detection_area: Area2D,context_teammate_detection_area: Area2D):
	player = context_player
	ball = context_ball
	opponent_detection_area = context_opponent_detection_area
	teammate_detection_area = context_teammate_detection_area

func process_ai() ->void:
	if Time.get_ticks_msec() - time_since_last_ai_tick > AI_TICK_FREQUENCY:
		time_since_last_ai_tick = Time.get_ticks_msec()
		perform_ai_movement()
		perform_ai_decisions()
		
func perform_ai_movement() -> void:
	pass

func perform_ai_decisions() -> void:
	pass

func get_bicircular_weight(position:Vector2,center_target:Vector2,inner_circle_radius:float,inner_circle_weight:float,outer_circle_radius:float,outer_circle_weight:float) -> float:
	var distance_to_center := position.distance_to(center_target)
	if distance_to_center > outer_circle_radius:
		return outer_circle_weight
	elif distance_to_center < inner_circle_radius:
		return inner_circle_weight
	else:
		var distance_to_inner_radius := distance_to_center- inner_circle_radius
		var close_range_distance := outer_circle_radius - inner_circle_radius
		return lerpf(inner_circle_weight,outer_circle_weight, distance_to_inner_radius / close_range_distance)


func is_ball_possessed_by_opponent() -> bool:
	return ball.carrier != null and ball.carrier.country != player.country

func is_ball_carried_by_teammate() -> bool:
	if ball.carrier == null and ball.carrier != player:
		return false
	
	return ball.carrier.country == player.country

func has_opponents_nearby() -> bool:
	var players := opponent_detection_area.get_overlapping_areas()
	return players.find_custom(func(p:Player): return p.country != player.country) > -1
